package net.jalbum.filters.arty.effects;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.DataBuffer;
import java.io.IOException;
import java.util.Map;
import java.util.Random;
import javax.imageio.ImageIO;

/**
 *
 * @author Sarah Schölzel
 */
public class ThistleFilter extends ArtySupport{

    private int[] pixels;

    public ThistleFilter() {
    }

    @Override
    public String getName() {
        return "Thistle";
    }

    @Override
    public String getDescription() {
        return "gives your photo an old, used look";
    }

    @Override
    public BufferedImage filter(BufferedImage bi, Map map) {
        return filter(bi);
    }

    @Override
    public BufferedImage filter(BufferedImage bi) {
        bi = super.filter(bi);

        BufferedImage dstImg = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());

        //new colorLayer
        BufferedImage tmp = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
        Graphics2D gtmp = tmp.createGraphics();
        gtmp.setColor(new Color(17, 24, 66));
        gtmp.fill(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));

        //new colorLayer
        BufferedImage tmp2 = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
        Graphics2D gtmp2 = tmp2.createGraphics();
        gtmp2.setColor(new Color(220, 227, 84));
        gtmp2.fill(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));

        //Blending Mode: Lighten, 55%
        gtmp2.setComposite(BlendComposite.getInstance(BlendComposite.BlendingMode.LIGHTEN, 0.55f));
        gtmp2.drawImage(tmp, 0, 0, bi.getWidth(), bi.getHeight(), null);

        //texture
        BufferedImage texture = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
        try {
            texture = ImageIO.read(getClass().getResource("texture_old_square.jpg"));
        } catch (IOException ex) {
            new RuntimeException(ex);
        }

        //rotate Texture to get difference
        Random generator = new Random();
        int random = generator.nextInt(4);
        double angle = 0;
        for (int i = 0; i < random; i++) {
            angle += 90;
        }
        angle = angle * Math.PI / 180.0;

        AffineTransform aff = new AffineTransform();
        aff.rotate(angle, texture.getWidth() / 2.0, texture.getHeight() / 2.0);

        BufferedImageOp bio = new AffineTransformOp(aff, AffineTransformOp.TYPE_BILINEAR);
        texture = bio.filter(texture, null);

        //Blending Mode: Multiply, 75%
        tmp = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
        gtmp = tmp.createGraphics();
        gtmp.drawImage(texture, 0, 0, bi.getWidth(), bi.getHeight(), null);
        gtmp.setComposite(BlendComposite.getInstance(BlendComposite.BlendingMode.MULTIPLY, 0.75f));
        gtmp.drawImage(tmp2, 0, 0, bi.getWidth(), bi.getHeight(), null);

        //Blending Mode: Overlay, 65%
        Graphics2D gdst = dstImg.createGraphics();
        gdst.drawImage(bi, 0, 0, null);
        gdst.setComposite(BlendComposite.getInstance(BlendComposite.BlendingMode.OVERLAY, 0.65f));
        gdst.drawImage(tmp, 0, 0, bi.getWidth(), bi.getHeight(), null);

        //Border
        dstImg = adjustBorder(dstImg, STRONG_EFFECT_STRENGTH);

        return dstImg;
    }
}
